package com.abe.bathsystem.widget.dialog;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.yet.campusdev.R;

import com.abe.bathsystem.base.ManagerLocalData;
import com.abe.bathsystem.entity.ReqConstant;
import com.abe.bathsystem.entity.http.HttpBathPosStateEntity;
import com.abe.bathsystem.entity.http.HttpUserEntity;
import com.abe.bathsystem.widget.OnOperateListener;
import com.abe.libcore.utils.normal.EmptyUtils;
import com.abe.libcore.utils.normal.ToastUtils;

public class BathControlDialog extends BaseDialog implements View.OnClickListener {

    public interface OnBathControlListener {
        void onLock();

        void onStartBath();

        void onEndBath();

        void onControlLocker();
    }

    private OnBathControlListener listener;
    private HttpUserEntity userEntity;
    private HttpBathPosStateEntity posStateEntity;

    public BathControlDialog(@NonNull Context context, OnOperateListener operateListener, HttpUserEntity userEntity, HttpBathPosStateEntity posStateEntity) {
        super(context, operateListener);
        if (EmptyUtils.isEmpty(userEntity)) return;
        this.userEntity = userEntity;
        this.posStateEntity = posStateEntity;
    }

    public BathControlDialog(@NonNull Context context, HttpUserEntity userEntity, HttpBathPosStateEntity posStateEntity) {
        super(context);
        if (EmptyUtils.isEmpty(userEntity)) return;
        this.userEntity = userEntity;
        this.posStateEntity = posStateEntity;
    }

    public void setListener(OnBathControlListener listener) {
        this.listener = listener;
    }

    private LinearLayout item_btn_user_stop_bath, item_btn_pos_locker, item_btn_user_bath_status_layout;
    private TextView item_txt_user_bath_status;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.item_view_v3_control_bath_control);
        TextView itemUserName = findViewById(R.id.item_user_name);
        itemUserName.setText(userEntity.getUser().getName());
        LinearLayout item_btn_user_lock = findViewById(R.id.item_btn_user_lock);
        LinearLayout item_btn_user_start_bath = findViewById(R.id.item_btn_user_start_bath);
        item_btn_user_stop_bath = findViewById(R.id.item_btn_user_stop_bath);
        item_btn_pos_locker = findViewById(R.id.item_btn_pos_locker);
        item_btn_user_bath_status_layout = findViewById(R.id.item_btn_user_bath_status_layout);
        item_txt_user_bath_status = findViewById(R.id.item_txt_user_bath_status);
        item_btn_user_lock.setOnClickListener(this);
        item_btn_user_start_bath.setOnClickListener(this);
        item_btn_user_stop_bath.setOnClickListener(this);
        item_btn_pos_locker.setOnClickListener(this);
        if (posStateEntity == null) {
            dismiss();
            return;
        }
        //只有正在洗浴状态下显示停止洗浴按钮
        item_btn_user_stop_bath.setVisibility(posStateEntity.getStatus() == ReqConstant.BATH_POS_STATE_BATHING ? View.VISIBLE : View.GONE);
        item_btn_pos_locker.setVisibility(ManagerLocalData.isSupportLocker() ? View.VISIBLE : View.GONE);
        boolean isBeforeBath = posStateEntity.getStatus() == ReqConstant.BATH_POS_STATE_NO || posStateEntity.getStatus() == ReqConstant.BATH_POS_STATE_RESERVE;
        item_btn_user_bath_status_layout.setVisibility(isBeforeBath ? View.VISIBLE : View.GONE);
        int x = (posStateEntity.getPos() + 1);
        String beforeBath = "正在开始" + x + "号浴位";
        String bath = x + "号浴位已开启，请至对应浴位使用";
        item_txt_user_bath_status.setText(isBeforeBath ? beforeBath : bath);
        switch (posStateEntity.getStatus()) {
            case ReqConstant.BATH_POS_STATE_NO:
            case ReqConstant.BATH_POS_STATE_RESERVE:
                if (listener != null) {
                    listener.onStartBath();
                }
                break;
        }
    }

    public void updateStatus(int pos, int status, String message) {
        if (pos != posStateEntity.getPos()) return;
        if (status == ReqConstant.BATH_POS_START_FAIL) {
            item_txt_user_bath_status.setText(message);
            return;
        }
        item_btn_user_stop_bath = findViewById(R.id.item_btn_user_stop_bath);
        item_btn_pos_locker = findViewById(R.id.item_btn_pos_locker);
        item_btn_user_bath_status_layout = findViewById(R.id.item_btn_user_bath_status_layout);
        item_txt_user_bath_status = findViewById(R.id.item_txt_user_bath_status);
        //只有正在洗浴状态下显示停止洗浴按钮
        item_btn_user_stop_bath.setVisibility(View.GONE);
        item_btn_pos_locker.setVisibility(ManagerLocalData.isSupportLocker() ? View.VISIBLE : View.GONE);
        boolean isBeforeBath = status == ReqConstant.BATH_POS_STATE_NO || status == ReqConstant.BATH_POS_STATE_RESERVE;
        item_btn_user_bath_status_layout.setVisibility(View.VISIBLE);
        int x = (posStateEntity.getPos() + 1);
        String beforeBath = "正在开始" + x + "号浴位";
        String bath = x + "号浴位已开启，请至对应浴位使用";
        item_txt_user_bath_status.setText(isBeforeBath ? beforeBath : bath);
    }

    @Override
    public void onClick(View view) {
        if (listener != null) {
            switch (view.getId()) {
                case R.id.item_btn_user_lock:
                    listener.onLock();
                    break;
                case R.id.item_btn_user_start_bath:
                    listener.onStartBath();
                    break;
                case R.id.item_btn_user_stop_bath:
                    listener.onEndBath();
                    break;
                case R.id.item_btn_pos_locker:
                    listener.onControlLocker();
                    break;
            }
        } else {
            ToastUtils.show("请绑定回调监听");
        }
        dismiss();
    }
}